window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		var curPage = 1;
		var pageSize = 10;
		var totalPage = 0;
		var keyword = U.getParam("keyword");
		var location_id = 0;
		var person_type = "个人";
		var first = true;
		var town = $(".poor_screen select").eq(0); //镇ID
		var village = $(".poor_screen select").eq(1); //区ID
		var reason = ""; 		//导致贫困的原因
		var requirement = ""; 	//帮扶要求
		var p1;
		var search_type = U.getParam("search_type");
		var specialId = U.getParam("specialId");		//specialId-2跳转到专项帮扶对象 不传跳转到精准贫困户
		if(specialId == null){
			specialId = 0;
		}
		U.fillHead();
		U.fillFooter();
		initPage();

		function initPage() { //初始化页面
			keyword = $("#title .search>input").val();
			U.loadOpt(false);
			get_location();								//获取地区
			search_type = $("#title select").val();
			$(".term>span:last-child").empty();			//清空搜索条件
			initEvent();
			type_btn_click();
			if(search_type == "团队名称" || search_type == "全部") {
				$(".type_result>span:last-child>span").eq(0).click();
			}
			if(search_type == "资讯") {
				$(".type_result>span:last-child>span").eq(3).click();
			}
			if(search_type == "贫困户") {
				$(".type_result>span:last-child>span").eq(1).click();
				if(U.getParam("url") != null) {
					$("." + U.getParam("url")).click();

					p1 = $("." + U.getParam("url")).parent().parent().hide();
					console.info(p1);
					if($(p1).attr("class") == "requirementList") {
						$(p1).hide();
					}

				} else {}
			}
			if(search_type == "项目") {
				$(".type_result>span:last-child>span").eq(2).click();
			}
		}

		function type_btn_click() {
			$(".type_result>span:last-child>span").click(function() {
				switch($(this).index()) {
					case 0:
						curPage = 1;
						pageSize = 10;
						get_teamList();
						//get_person_list();
						break;
					case 1:
						curPage = 1;
						pageSize = 10;
						get_poor_list();
						break;
					case 2:
						curPage = 1;
						pageSize = 10;
						get_project_list();
						break;
					case 3:
						curPage = 1;
						pageSize = 5;
						get_policy_list();
						break;
				}
			});
		}

		function reset_width() {
			$(".personal_list>div").width((($(".personal_list").width()) - 30 * 3) / 2);
			$(".project_list>div").width((($(".project_list").width()) - 30 * 3) / 2);
			$(".team_list>div").width((($(".team_list").width()) - 30 * 3) / 2);
			$(".project_list>div").width((($(".project_list").width()) - 30 * 3) / 2);
			$(".entreprise_list>div").width((($(".entreprise_list").width()) - 30 * 3) / 2);
		}

		function getParams() {
			reason = "";
			requirement = "";
			$(".term>span:last-child>span").each(function() {
				if($("." + $(this).attr("type") + "").parent().parent().attr("class") == "reasonList") {
					reason += " " + $(this).attr("text");
				} else {
					requirement += " " + $(this).attr("text");
				}
			});
			get_poor_list();
		}

		function initEvent() {
			//点击致贫原因、帮扶要求
			$(".reasonList>span:last-child>span,.requirementList>span:last-child>span").unbind("click").click(function() {
				var this_text = $(this).hide().text();
				var this_type = $(this).parent().parent().attr("class");
				if(this_type == "reasonList") {} else {}
				//添加搜索条件
				$(".term>span:last-child").append("<span text='" + this_text + "' type='" + $(this).attr("class") + "' >" + ("") + "" + this_text + "<span>X</span></span>");
				getParams();

				$(".term>span:last-child>span>span").unbind("click").click(function() {
					if($(p1).attr("class") == $("." + $(this).parent().attr("type") + "").parent().parent().attr("class")) {
						return false;
					}
					$("." + $(this).parent().attr("type") + "").show();
					if($(this).parent().attr("type") == "reasonList") {} else {}

					$(this).parent().remove();
					getParams();

				});
				$(".term>span:last-child>span").unbind("mousemove").bind("mousemove", function() {
					$(this).css("border-color", "rgb(224, 61, 67)");
					$(this).find("span").css("color", "red");
				});
				$(".term>span:last-child>span").unbind("mouseleave").bind("mouseleave", function() {
					$(this).css("border-color", "#E8E8E8");
					$(this).find("span").css("color", "black");
				});
			});
			$(".type_result>span:last-child>span").unbind("click").click(function() {
				$(".type_result>span:last-child>span").css("background-color", "white");
				$(".type_result>span:last-child>span").css("color", "black");
				$(this).css("background-color", "#E03D43");
				$(this).css("color", "white");
				$(".result>div").hide();
				$(".help_the_poor_screen").hide();
				$(".poor_screen").hide();
				switch($(this).index()) {
					case 0:
						$(".help_the_poor_screen").show();
						$("#paging").show();
						break;
					case 1:
						$(".poor_screen").show();
						$("#paging").show();
						break;
					case 2:
						$("#paging").show();
						break;
					case 3:
						$("#paging").show();
						break;
				}
			});
			$(".help_the_poor_screen>div").unbind("click").click(function() {
				$(".help_the_poor_screen>div").css("border-color", "#CCCCCC");
				$(".help_the_poor_screen>div").css("color", "black");
				$(".help_the_poor_screen>div").css("background-color", "white");
				$(this).css("border-color", "#E03D43");
				$(this).css("color", "white");
				$(this).css("background-color", "#E03D43");
				$(".result>div").hide();
				if($(this).text() == "团队") {
					$(".team_list").show(100);
					get_teamList();
					$("#paging").show();
					reset_width();
				}
				if($(this).text() == "个人") {
					$(".personal_list").show();
					get_person_list();
					$("#paging").show();
					reset_width();
				}
				if($(this).text() == "企业") {
					$(".entreprise_list").show();
					get_entrepriseList();
					$("#paging").show();
					reset_width();
				}
			});
		}

		function details_click() {
			$(".policy_list>div").unbind("click").click(function() {
				window.open("Policy_details.html?id=" + $(this).attr("id"));
			});

			$('.team_list>div,.entreprise_list>div').unbind('click').click(function() { //团队详情/企业详情
				window.open("team_or_entreprise_details.html?type=" + $(this).attr("type") + "&userId=" + $(this).attr("userId"));
			});

			$(".personal_list>div").unbind("click").click(function() {
				window.open("helper_info.html?id=" + $(this).attr("id") + "&userId=" + $(this).attr("userId"));
			});
			$(".project_list>div").click(function() {
				window.open("project_details.html?id=" + $(this).attr("id"));
			});
			$(".poor_list .poor_details").unbind("click").click(function() {
				/*U.laodPoor_view(true, {
					"id": $(this).attr("id")
				});*/
				window.open('poor_peoleDetail.html?id='+$(this).attr('id') + "&specialId=" + specialId);			//跳转贫困者详情
			});
		}
		//获取项目列表
		function get_project_list() {
			var params = {
				curPage: curPage,
				pageSize: pageSize,
				name: keyword
			};
			U.loadOpt(true);
			U.ajax("/project/list", params, function(json) {
				if(json.success) {
					$(".project_list").empty();
					$(".project_list").hide();

					$(json.models.projectList).each(function() {
						var p = "<div id='" + this.id + "'><div class='p_title'>" + this.name + "</div><div class='p_content'>" + this.title + "</div><div class='p_time'>" + U.getMyDate(this.gmtCreate) + "</div></div>";
						$(".project_list").append(p);
					});
					$(".project_list").show(100, function() {
						reset_width();
					});
					paging_handle(json.models.page, get_project_list); //分页调用方法

					details_click();
					U.loadOpt(false);

				} else {
					M.toast(false, json.errorMsg);
				}

			});
		}
		//获取资讯
		function get_policy_list() {
			var params = {
				curPage: curPage,
				pageSize: pageSize,
				title: keyword,
				newsTypeId:0,
				contentType:2
			};
			U.loadOpt(true);
			U.ajax("/news/queryByType", params, function(json) {
				if(json.success) {
					$(".policy_list").empty();
					$(".policy_list").hide();
					totalPage = json.models.page.totalPage;
					$(json.models.newsDeList).each(function() {
						var img = U.loadfail;
						if(U.getImg(this.images).length > 0) {
							img = U.getImg(this.images)[0];
						}
						var policy = "<div id='" + this.id + "'><div class='zx_img'><img src='" + img + "'></div><div class='zx_content'><div class='zx_title'>" + this.title + "</div><div class='zx_title_f'>" + this.subtitle + "</div><div class='continue'>继续阅读 &gt;</div></div><div class='zx_time'>" + U.getMyDate(this.gmtCreate) + "</div></div>";
						$(".policy_list").append(policy);
					});
					$(".policy_list").show(200, function() {
						reset_width();
					});
					paging_handle(json.models.page, get_policy_list);
					details_click();
					U.loadOpt(false);
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		//获取帮扶队员列表
		function get_person_list() {
			$(".personal_list").empty();
			$(".personal_list").hide();

			var params = {
				curPage: curPage,
				pageSize: pageSize,
				name: keyword
			};
			U.loadOpt(true);
			U.ajax("/person/list", params, function(json) {
				U.loadOpt(false);
				if(json.success) {
					$(json.models.personList).each(function() {
						var person = "<div userId='" + this.userId + "' type='2' ><div class='g_name'><span>" + this.name + "</span><img src='../images/icons/fhbbzz.png'>符合帮扶资质</div><div><img src='../images/icons/icon_c.png'><span>所在城市：" + this.city + "</span></div><div><img src='../images/icons/icon_b.png'><span>" + U.formPhone(this.mobile) + "</span></div><img class='g_jinru' src='../images/icons/icon_jinru.png'></div>";
						$(".personal_list").append(person);
					});
					paging_handle(json.models.page, get_person_list);
					$(".personal_list").show(100, function() {
						reset_width();
					});
					details_click();
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		//获取帮扶团体列表
		function get_teamList() {
			$('.team_list').html('');
			var params = {
				curPage: curPage,
				name: keyword
			};
			U.ajax('/team/teamList', params, function(json) {
				if(json.success) {
					var result = json.models.userTeamList;
					$(result).each(function() {
						$('.team_list').append('<div userId=' + this.userId + ' type="2"><div class="g_name"><span>' + this.name + '</span><img src="../images/icons/fhbbzz.png">符合帮扶资质</div><div class="profile"><span>团队简介：</span><span>' + this.intro + '</span></div><div><img src="../images/icons/icon_b.png"><span>' + U.formPhone(this.connectPhone) + '</span></div><img class="g_jinru" src="../images/icons/icon_jinru.png"></div>');
					});
					paging_handle(json.models.page, get_teamList);
					$(".team_list").show(100, function() {
						reset_width();
					});
					details_click();
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}

		// 获取帮扶企业列表
		function get_entrepriseList() {
			$(".entreprise_list").html('');
			var params = {
				curPage: curPage,
				name: keyword
			};
			U.ajax('/entreprise/list', params, function(json) {
				if(json.success) {
					var result = json.models.entrepriseList;
					$(result).each(function() {
						var person = "<div userId='" + this.userId + "'  type='1'><div class='g_name'><span>" + this.name + "</span><img src='../images/icons/fhbbzz.png'>符合帮扶资质</div><div><img src='../images/icons/icon_c.png'><span>所在城市：" + this.address + "</span></div><div><img src='../images/icons/icon_b.png'><span>" + U.formPhone(this.tel) + "</span></div><img class='g_jinru' src='../images/icons/icon_jinru.png'></div>";
						$(".entreprise_list").append(person);
					});
					paging_handle(json.models.page, get_entrepriseList);
					$(".entreprise_list").show(100, function() {
						reset_width();
					});
					details_click();
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		//获取贫困户列表
		function get_poor_list() {
			var provinceId = $(town).val();
			var cityId = $(village).val();
			if(provinceId == null) provinceId = 0;
			if(cityId == null) cityId = 0;
			var params = {
				curPage: curPage,
				pageSize: pageSize,
				provinceId: provinceId,
				cityId: cityId,
				userId: specialId,				//userId-1查询结对帮扶用户 ，-2 专项帮扶用户 -3 查询全部 不传值默认查询结对帮扶用户
				reason: reason,
				requirement: requirement,
				name:keyword
			};
			U.loadOpt(true);
			$(".poor_list").empty();
			$(".poor_list").hide();
			U.ajaxAsync("/poverty/list", params, function(json) {
				U.loadOpt(false);
				if(json.success) {
					if(first) {			//第一次加载致贫原因和帮扶要求
						var reasonList = json.models.reasonList;
						var requirementList = json.models.requirementList;
						$(".search_reasonList").empty();
						$(".search_requirementList").empty();
						$(requirementList).each(function() {
							$(".search_requirementList").append("<span class='requirementList" + this.id + "'>" + this.name + "</span>");
						});
						$(reasonList).each(function() {
							$(".search_reasonList").append("<span class='reasonList" + this.id + "'>" + this.name + "</span>");
						});
						initEvent();
						first = false;
					}
					$(json.models.povertyObjList).each(function() {
						var images = U.loadfail;
						var _isPoverty;							//是否结对
						//var poor="";
						if(U.getImg(this.images).length > 0) {
							images = U.getImg(this.images)[0];
						}
						if(specialId == -2){
							$('.poor_list').append("<div> <div class='poor_img'> <img src='" + images + "'/></div> <div class='poor_name'>" + this.name + "</div><div class='poor_address'><span>住址：</span><span>" + this.address + "</span></div><div class='poor_reason'><span>帮扶需求：</span><span>" + this.requirement + "</span></div><span class='poor_details' id='" + this.id + "'>详情</span></div>");
						}
						else {
							if(this.isPoverty == 1){	//1:已结对
								_isPoverty = '../images/icons/twinning.png'
							}
							else if(this.isPoverty == 2){	//2：未结对
								_isPoverty = '../images/icons/not_twinning.png'
							}
							//var poor = ;
							$(".poor_list").append("<div> <div class='poor_img'> <img src='" + images + "'/></div> <div class='poor_name'>" + this.name + "</div><div class='poor_address'><span>住址：</span><span>" + this.address + "</span></div><div class='poor_reason'><span>帮扶需求：</span><span>" + this.requirement + "</span></div><div class='poverty'><img src='"+_isPoverty+"' ></div><span class='poor_details' id='" + this.id + "'>详情</span></div>");
						}
					});
					$(".poor_list").show(100, function() {
						reset_width();
					});
					paging_handle(json.models.page, get_poor_list);
					details_click();
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		//获取地区
		function get_location() {
			village = $(village)[0];
			town = $(town)[0];
			U.ajaxget("location/listByPid/" + location_id, {}, function(json) {
				if(location_id == 0) {
					$(town).empty();
					town.options.add(new Option("全部", 0));
					$(json.models.locationList).each(function() {
						town.options.add(new Option(this.name, this.id));
					});
					$(town).unbind("change").bind("change", function() {
						$(village).empty();
						if($(town).val() != 0) {
							village.options.add(new Option("全部", 0));
						}
						location_id = $(this).val();
						get_location();
						get_poor_list();
						$("#paging .goto_page>input").val(1);
					});
					location_id = 1;
					//					get_lcoation();
					//					get_poor_list();
					village.options.add(new Option("全部", 0));
				} else {
					$(json.models.locationList).each(function() {
						village.options.add(new Option(this.name, this.id));
					});
					$(village).unbind("change").bind("change", function() {
						get_poor_list();
						$("#paging .goto_page>input").val(1);

					});
				}
			});
		}
		//分页
		function paging_handle(page, method) {
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;

			if(lastsize >= totalPage) {
				lastsize = totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		}
	});
};